//
//  WBEmoticonKeyboardView.swift
//  Weibo
//
//  Created by xu on 2017/3/30.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

class WBEmoticonKeyboardView: UIView {

   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 供外界调用的方法
     func reloadPageViewData(){
        // 刷新指定的indexPath
          let indexPath = IndexPath(item: 0, section: 0)
        emoticonPageView.reloadItems(at: [indexPath])
    
    }
    
  
    //MARK: 设置视图
     private func setupUI(){
      backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
      addSubview(emoticonBottomView)
      addSubview(emoticonPageView)
        addSubview(pageControl)
        
        emoticonBottomView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(35)
        }
        
        emoticonPageView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp.top)

        }
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView).offset(10)
        }


        
        // 监听底部视图的按钮点击回调
        emoticonBottomView.closure = {[weak self] type -> () in

            switch type {
            case .recent:
                print("最近")
            case .normal:
                print("默认")
            case .emoji:
                print("Emoji")
            case .lxh:
                print("浪小花")
        
            }
            
             // 计算indexPath
            let indexPath = IndexPath(item: 0, section: type.rawValue - 2000)
             // 滚动表情视图到指定位置
            self?.emoticonPageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
        }
        // 默认滚动到第一组第0页
        // 计算indexPath
          let indexPath = IndexPath(item: 0, section:1)
        
        // 通过主线程异步解决滚动不到指定组和指定行的bug
        DispatchQueue.main.async {
            self.emoticonPageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
        }

        
    }

    //MARK: 懒加载控件
    // 底部视图
    fileprivate lazy var emoticonBottomView:WBEmoticonBottomView = WBEmoticonBottomView()
    
    //表情视图
    fileprivate lazy var emoticonPageView:WBEmoticonPageView = {
        let view = WBEmoticonPageView()
        // 弹簧效果
        view.bounces = false
        view.isPagingEnabled = true
        view.delegate = self
        //背景颜色和键盘颜色一样
        view.backgroundColor = self.backgroundColor
        // 隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        return view
        
    }()
    
    //pageControl
    fileprivate lazy var pageControl: UIPageControl = {
        let view = UIPageControl()
        view.isUserInteractionEnabled = false
        //设置page一个的时候为隐藏
        view.hidesForSinglePage = true
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
        // 默认
        view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")

        
         return view
    }()
}


//MARK:UICollectionViewDelegate
extension WBEmoticonKeyboardView:UICollectionViewDelegate {
     func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //当前的x值获取
        let centerX = ScreenWidth * 0.5+scrollView.contentOffset.x
          // 得到cell上一个点
        let center = CGPoint(x: centerX, y: 0)
         // 通过cell上的一点的到对应的indexPath
        if let indexPath = emoticonPageView.indexPathForItem(at: center) {
             emoticonBottomView.setupCurrentButton(tag: indexPath.section)
            
            //设置跟着联动
            pageControl.numberOfPages = WBEmoticonTools.shared.allEmoticons[indexPath.section].count
            pageControl.currentPage = indexPath.item

        }
        
        
    }
}
